<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Category extends Controller
{
    /**
     *  分类列表显示
     *
     * @return \think\Response
     */
    public function index()
    {
        $article_class = Db::name('article_class')->select();
        $tree = $this->article_class_list($article_class,0,0);
        return view('index',[
            'article_class'=>$tree
        ]);
    }

    /**
     * 遍历分类信息
     */
    public function article_class_list($arr,$id,$step)
    {
        static  $tree = [];
        foreach($arr as $val)
        {
            if($val['parent_id'] == $id)
            {
                $flg = str_repeat('└―',$step);
                $val['class_name'] = $flg.$val['class_name'];
                $tree[] = $val;
                $this->article_class_list($arr , $val['id'] ,$step+1);  
            }
        }
        return $tree;
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $article_class = Db::name('article_class')->select();
        $tree = $this->article_class_list($article_class,0,0);
        return view('create',[
            'article_class'=>$tree
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request,$class_name,$class,$add_name)
    {
        $class_name = trim($class_name,'└―');
        $list = Db::name('article_class')->where('class_name',$class_name)->find();
        // dump($list);die;
        if($class == '同级')
        {
            $data = ['class_name'=>$add_name,'parent_id'=>$list['parent_id']];
            $status = Db::name('article_class')->insert($data);
        }else{
             $data = ['class_name'=>$add_name,'parent_id'=>$list['id']];            
            $status = Db::name('article_class')->insert($data);     
        }

        if($status == 0)
        {
            echo "<script>alert('添加失败');</script>";
        }else{
            $this->redirect('admin/Category/index');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        // dump($id);die;
        $list = Db::name('article_class')->where('id',$id)->find();
        $data =['id'=>$list['id'],'class_name'=>$list['class_name']];
        return json($data);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id,$name_class)
    {
        // dump($id);
        // dump($name_class);die;
        $update = Db::name('article_class')->where('id',$id)->setField('class_name',$name_class);
        if($update == 1)
        {
            $data = Db::name('article_class')->where('id',$id)->value('class_name'); 
            $arr = ['class_name'=>$data];
            return json($arr);
        }else{
            echo "<script>alert('修改失败');</script>";
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id,$status)
    {
        if($status == 0){
            $data = Db::name('article_class')->where('id',$id)->find();
            // dump($data);die;
            $arr = ['id'=>$data['id'],'class_name'=>$data['class_name']];
            return json($arr);
        }else{
            Db::name('article_class')->where('id',$id)->delete();       
            $this->del_class($id,0);
            $arr = ['info'=>'删除成功'];
            return json($arr);
        }
    }

    /**
     * 删除遍历分类
     */
    public function del_class($id,$status)
    { 
        $data = Db::name('article_class')->select();      
        if($status == 0){
            foreach($data as $val)
            {
                if($val['parent_id'] == $id)
                {
                    $list = Db::name('article_class')->where('parent_id',$val['parent_id'])->find();
                    Db::name('article_class')->where('parent_id',$val['parent_id'])->delete();
                }
            }
        }
    }
}
